package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import java.util.List;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.ChangeIsReadReq;
import sz.szsmk.citizencard.model.network.bean.request.NoticeDelItemReq;
import sz.szsmk.citizencard.model.network.bean.request.NoticeListReq;
import sz.szsmk.citizencard.model.network.bean.request.NoticeMainReq;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.NoticeListRes;
import sz.szsmk.citizencard.model.network.bean.response.NoticeMainres;
import sz.szsmk.citizencard.mvp.contract.INewsContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/6/13 14:04
 * description:
 */
public class NewsPresenter extends IBasePresenter
        implements INewsContact.INewsPresenter<INewsContact.INewsView> {
    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public NewsPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void getMainNotice() {
        NoticeMainReq noticeMainReq = new NoticeMainReq();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        noticeMainReq.setAccess_token(access_token);
        noticeMainReq.setDevice_id("123");
        noticeMainReq.setSign("asdf");
        String toJson = new Gson().toJson(noticeMainReq);
        L.e(Constant.TAG, "noticeMainReq----->" + toJson);
        mNetworkDateSource.getMainNotice(noticeMainReq, new SubscribeResult<NoticeMainres>() {
            @Override
            protected void onOk(NoticeMainres response) {
                if (response.getMainPageInfoList() != null) {
                    ((INewsContact.INewsView) mView).getMainNoticeSucc(response.getMainPageInfoList());
                }
            }

            @Override
            protected void onFailure(NoticeMainres response) {
                super.onFailure(response);

                ((INewsContact.INewsView) mView).getMainNoticeError(response.getMessage());
            }

            @Override
            protected void onNext(NoticeMainres response) {
                super.onNext(response);
                ((INewsContact.INewsView) mView).getMainNoticeError(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

                ((INewsContact.INewsView) mView).getMainNoticeError(throwable.getMessage());
            }
        });
    }

    @Override
    public void queryAndSearch(String word) {
        NoticeListReq noticeListReq = new NoticeListReq();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        noticeListReq.setAccess_token(access_token);
        noticeListReq.setDevice_id("47");
        noticeListReq.setKeyWord(word);
        noticeListReq.setMessageType("0100");
        noticeListReq.setQueryType("2");
        noticeListReq.setSign("123");
        mNetworkDateSource.getNotcieList(noticeListReq, new SubscribeResult<NoticeListRes>() {
            @Override
            protected void onOk(NoticeListRes response) {
                List<NoticeListRes.MessageDetailListBean> detailList = response.getMessageDetailList();
                if (detailList != null) {
                    ((INewsContact.INewsView) mView).queryAndSearchSucc(detailList);
                }
            }

            @Override
            protected void onFailure(NoticeListRes response) {
                super.onFailure(response);
                ((INewsContact.INewsView) mView).queryAndSearchFail(response);
            }

            @Override
            protected void onNext(NoticeListRes response) {
                super.onNext(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });


    }

    @Override
    public void changeIsRead(String id, String type) {
        ChangeIsReadReq changeIsReadReq = new ChangeIsReadReq();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        changeIsReadReq.setAccess_token(access_token);
        changeIsReadReq.setDevice_id("adfaf");
        changeIsReadReq.setSign("adsfa");
        changeIsReadReq.setId(id);
        changeIsReadReq.setType(type);
        String json = new Gson().toJson(changeIsReadReq);
        L.e(Constant.TAG, "changeIsRead---->" + json);
        mNetworkDateSource.changesIsRead(changeIsReadReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {

            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void delItem(String messageType) {
        NoticeDelItemReq noticeDelItemReq = new NoticeDelItemReq();
        noticeDelItemReq.setMessageType(messageType);
        String json = new Gson().toJson(noticeDelItemReq);
        L.e(Constant.MTAG, "消息删除条目---->" + json);
        mNetworkDateSource.deleteMessageTypeByUser(noticeDelItemReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                L.e(Constant.MTAG,"------1");
                ((INewsContact.INewsView) mView).delItemSucc();
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                L.e(Constant.MTAG,"------2");
                ((INewsContact.INewsView) mView).delItemFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                L.e(Constant.MTAG,"------3");
                ((INewsContact.INewsView) mView).delItemFail(throwable.getMessage());
            }
        });

    }

}
